/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 *
 * \file
 * \brief Header file for defining common types.
 * \author Antti Maula <antti.maula@tkk.fi>
 */
#ifndef _MACI_LAUNCHER_LAUNCHERTYPES_HPP_
#define _MACI_LAUNCHER_LAUNCHERTYPES_HPP_
#include <string>
#include <vector>

/** Vector of strings.
 */
typedef std::vector<std::string> TStringArray;


/** Machine configuration container.
 */
struct SMachineConfEntry
{
  SMachineConfEntry() 
    : name(),
      description(),
      GIMnet_host(),
      GIMnet_port(0),
      GIMnet_name(),
      MaCIGroup(),
      workdir(),
      moduleFiles(),
      modulePatterns(),
      extbinFiles(),
      extbinPatterns() {}
  void Clear(void) {
    name.clear();
    description.clear();
    GIMnet_host.clear();
    GIMnet_port = 0;
    GIMnet_name.clear();
    MaCIGroup.clear();
    workdir.clear();
    moduleFiles.clear();
    modulePatterns.clear();
    extbinFiles.clear();
    extbinPatterns.clear();
  }
  std::string name; ///< Name of machine
  std::string description; ///< Description of machine
  std::string GIMnet_host; ///< GIMnet host, or zerolen if not set.
  unsigned int GIMnet_port; ///< GIMnet port, or zero if not set.
  std::string GIMnet_name; ///< GIMnet name, or zero if not set.
  std::string MaCIGroup; ///< Default group for this machine
  std::string workdir; ///< Machine working directory
  TStringArray moduleFiles; ///< Array of module config files.
  TStringArray modulePatterns; ///< Array of module config patterns
  TStringArray extbinFiles; ///< Array of module config files.
  TStringArray extbinPatterns; ///< Array of module config patterns
};

/**
 * Flags to modify the launcher behaviour on specific modules.
 */
enum EModuleFlag {
  /// Don't pass in GIMnet parameters even defined.
  KModuleFlagIgnoreGIMnetParam          = (1<<0),
  
  /// Don't pass in MaCI parameters even defined.
  KModuleFlagIgnoreMaCIParam            = (1<<1),
  
  /// Don't pass in ModuleConfig parameters even defined.
  KModuleFlagIgnoreModuleConfigParam    = (1<<2),
  
  /// Don't pass in user-defined parameters even defined
  KModuleFlagIgnoreExtraParam           = (1<<3),

  /// Don't use the module working directory even set.
  KModuleFlagIgnoreWorkingDir           = (1<<4),

  /// If this is set, this module drops root privileges prior to run.
  KModuleFlagDropRootPrivileges         = (1<<5),

  /// If this is set; process is restarted.
  KModuleFlagRespawnProcess             = (1<<6)
};

/// Type for flag container.
typedef unsigned int TModuleFlags;


/** Module configuration container.
 */
struct SModuleConfEntry
{
  SModuleConfEntry()
    : name(),
      description(),
      executablePath(),
      buildCommand(),
      workingDirectory(),
      GIMnet_host(),
      GIMnet_port(0),
      GIMnet_name(),
      MaCIGroup(),
      startdelay(0),
      moduleParam(),
      moduleConf(),
      flags(0),
      respawnTime_ms(0) {}
  void Clear(void) {
    name.clear();
    description.clear();
    executablePath.clear();
    buildCommand.clear();
    workingDirectory.clear();
    GIMnet_host.clear();
    GIMnet_port = 0;
    GIMnet_name.clear();
    MaCIGroup.clear();
    startdelay = 0;
    moduleParam.clear();
    moduleConf.clear();
    flags = 0;
    respawnTime_ms = 0;
  }
  std::string name; ///< Name of module
  std::string description; ///< Description of module
  std::string executablePath; ///< Path to module executable
  std::string buildCommand; ///< Build command used when 'Build' requested.
  std::string workingDirectory; ///< Special working directory to change to before executing.

  std::string GIMnet_host; ///< GIMnet host, or zerolen if not set.
  unsigned int GIMnet_port; ///< GIMnet port, or zero if not set.
  std::string GIMnet_name; ///< GIMnet name, or zero if not set.

  std::string MaCIGroup; ///< MaCIGroup to use, zerolen if not set.
  unsigned int startdelay; ///< Start delay of process.

  TStringArray moduleParam; ///< Extra parameters to be passed
  std::string moduleConf; ///< Configuration file to be passed to module.

  TModuleFlags flags; ///< Entry flags.

  unsigned int respawnTime_ms; ///< Time in milliseconds before respawn.
};

typedef std::vector<SModuleConfEntry> TModuleConfEntryArray;

#endif
